<template>
  <div class="home">
    <el-container>
      <el-header>
        <div>
          <img class="header-img" src="../assets/logo.png" alt />
          <span>XX管理系统</span>
        </div>
        <el-button class="btn-exit-login" type="danger" @click="loginOut">退出系统</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse?'64px':'200px'">
          <div @click="toggleMenu()" :width="isCollapse?'64px':'200px'" class="menu-collapse">
              <img v-if='isCollapse' src="../assets/img/nav_open.png" alt="">
              <img v-else src="../assets/img/nav_close.png" alt="">
          </div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409eff"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
            :default-active="activePath"
          >
            <el-submenu :index="item.id+''" :key="item.id" v-for="(item) in datas">
              <template slot="title">
                <!-- <i class="el-icon-message"></i> -->
                <i :class="iconsObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <el-menu-item
                :index="'/home/'+childItem.path"
                :key="childItem.id"
                v-for="(childItem) in item.children"
                @click="saveActivePath('/home/'+childItem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{childItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view class="items"></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  created() {
    this.getMenus()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data() {
    return {
      token: window.sessionStorage.getItem('token'),
      datas: [],
      isCollapse: false,
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      activePath: ''
    }
  },
  methods: {
    loginOut() {
      window.sessionStorage.removeItem('token')
      this.$router.push({
        path: '/login'
      })
    },
    async getMenus() {
      const params = {
        url: '/menus',
        fullscreen: true
      }
      const res = await this.$send(params)
      if (res.meta.status === 200) {
        this.datas = res.data
      }
    },
    toggleMenu() {
      this.isCollapse = !this.isCollapse
    },
    saveActivePath(path) {
      window.sessionStorage.setItem('activePath', path)
    }
  }
}
</script>
<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}

.el-container {
  height: 100%;
}
.home {
  height: 100%;
}
.header-img {
  height: 50px;
}
.el-header {
  height: 60px;
  background: #373d41;
  display: flex;
  justify-content: space-between;
  color: rgb(255, 255, 255);
  font-size: 20px;
  align-items: center;
  > div {
    height: 50px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    > span {
      line-height: 50px;
      text-align: center;
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #333744;
  color: red;
  .el-menu{
    border-right: none;
  }
  .menu-collapse{
    width: 100%;
    height: 40px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    >img{
      width: 18px;
      height: 18px;
      margin-right: 22px;
    }
  }
  .el-menu-item-group,
  .el-menu-item,
  .el-submenu {
    background-color: #4a5064;
  }
  .iconfont{
    margin-right: 10px;
    font-size: 20px;
  }
}
.el-main {
    background-color: #E9EEF3;
    color: #333;
    margin: 0;
    padding: 0;
}
body > .el-container {
  margin-bottom: 40px;
}
</style>
